package com.bdqn.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.mapper.RightMapper;
import com.bdqn.mapper.RoleMapper;
import com.bdqn.pojo.Right;
import com.bdqn.pojo.Role;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * RoleServiceImpl
 *
 * @author LILIBO
 * @since 2023-12-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RightMapper rightMapper;

    /**
     * 获得所有角色列表
     *
     * @return
     * @Cacheable 首先查缓存有就返回，没有就执行方法，方法返回不为空添加缓存
     * value: 缓存名称，可以多个
     * key: 缓存的键值
     * condition: 满足条件的调用，结果才会被缓存（不指定时相当于true-缓存）
     * unless: 不合适的返回，结果不缓存（不指定时相当于false-缓存）
     * 总结: 当condition=false一定不缓存；当condition=true且unless=true不缓存；当condition=true且unless=false缓存
     */
    @Override
    @Cacheable(value = "role", key = "'getAll'", unless = "#result==null")
    public List<Role> getAllRole() {
        return super.list();
    }

    /**
     * 动态加载授权信息
     *
     * @return
     */
    @Override
    public Map<String, String> loadFilterChainDefinitions() {

        // 权限配置集合，必须使用LinkedHashMap(有序集合)
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();

        // 静态资源授权
        filterChainDefinitionMap.put("/css/**", "anon");
        filterChainDefinitionMap.put("/fonts/**", "anon");
        filterChainDefinitionMap.put("/images/**", "anon");
        filterChainDefinitionMap.put("/js/**", "anon");
        filterChainDefinitionMap.put("/localcss/**", "anon");
        filterChainDefinitionMap.put("/localjs/**", "anon");

        // 登录退出页面设置
        filterChainDefinitionMap.put("/login", "anon"); // 点击登录按钮时放行
        filterChainDefinitionMap.put("/logout", "logout"); // 点击退出按钮时放行

        // 静态授权：包括全部需要特定权限才能访问的资源（URL）
        /* filterChainDefinitionMap.put("/user/list", "perms[用户列表]");
        filterChainDefinitionMap.put("/user/add", "perms[用户添加]");
        filterChainDefinitionMap.put("/user/edit", "perms[用户编辑]");
        filterChainDefinitionMap.put("/user/del", "perms[用户删除]"); */

        // 动态设置全局资源权限
        List<Right> rights = rightMapper.getAllRight();
        for (Right right : rights) {
            if (right.getRightUrl() != null && !right.getRightUrl().trim().isEmpty()) {
                filterChainDefinitionMap.put(right.getRightUrl(), "perms[" + right.getRightCode() + "]");
            }
        }

        // 配置认证访问：其他资源(URL)必须认证通过才能访问
        filterChainDefinitionMap.put("/**", "authc"); // 必须放在过滤器链的最后面

        return filterChainDefinitionMap;
    }

    /**
     * 分页查询角色列表
     *
     * @param roleName 角色名称
     * @param page 分页对象
     * @return
     */
    @Override
    @Cacheable(value = "rolePage", key = "#page.getCurrent() + '-' + #page.getSize() + '-' + #roleName", condition = "#page.getCurrent() != null and #page.getSize() != null", unless = "#result == null")
    public Page<Role> getPage(String roleName, Page page) {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            qw.like("role_name", roleName);
        }
        return super.page(page, qw);
    }

}
